﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMovieSystem
{
    public partial class FrmAddMovie : Form
    {
        public FrmAddMovie()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 用于判断加载登录窗体
        /// 1：新增
        /// 2：修改
        /// </summary>
        public int n = 0;
        /// <summary>
        /// 影片对象
        /// </summary>
        public Movie movie = new Movie();

        #region 加载事件
        //加载事件
        private void FrmAddMovie_Load(object sender, EventArgs e)
        {

            if (n == 2)
            {
                textBox7.ReadOnly = true;
                this.Text = "修改影片";
                textBox7.Text = movie.MovieId;
                textBox1.Text = movie.MovieName;
                textBox2.Text = movie.EnglishName;
                textBox3.Text = movie.MovieType;
                textBox4.Text = movie.Country;
                textBox5.Text = movie.MovieDuration;
                textBox6.Text = movie.MoviePrice+"";
                dateTimePicker1.Value = movie.BeOnDate;
                textBox9.Text = movie.MoviePath;
                textBox10.Text = movie.VideoPath;
                textBox8.Text = movie.MovieAbout;
            }
        }
        #endregion

        #region 确定
        //确定
        private void button2_Click(object sender, EventArgs e)
        {
            if (CheckNull())
            {
                try
                {
                    movie.MovieId = textBox7.Text;
                    movie.MovieName = textBox1.Text;
                    movie.EnglishName = textBox2.Text;
                    movie.MovieType = textBox3.Text;
                    movie.Country = textBox4.Text;
                    movie.MovieDuration = textBox5.Text;
                    movie.MoviePrice = Convert.ToDouble(textBox6.Text);
                    movie.BeOnDate = dateTimePicker1.Value;
                    movie.MoviePath = textBox9.Text;
                    movie.VideoPath = textBox10.Text;
                    movie.MovieAbout = textBox8.Text;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message,"提示:");
                }
                if (n == 1)
                {
                    try
                    {
                        MovieManage.dirMovie.Add(movie.MovieId, movie);
                        MessageBox.Show("添加成功！", "提示:");
                    }
                    catch
                    {
                        MessageBox.Show("该影片编号已存在","提示：");
                        return;
                    }
                }
                if(n==2)
                    MessageBox.Show("修改成功！","提示:");
                this.Close();
            }
        }
        #endregion

        //退出
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 为空验证
        /// <summary>
        /// 为空验证
        /// </summary>
        /// <returns></returns>
        private bool CheckNull()
        {
            foreach (Control item in this.groupBox1.Controls)
            {
                if (item is TextBox)
                {
                    if (item.Text.Trim().Length < 1)
                    {
                        MessageBox.Show("请填写完整的影片信息！","提示：");
                        return false;
                    }
                }
            }
            return true;
        }
        #endregion
    }
}
